import 'dart:io';
import 'package:flutter/material.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'settings_page_controller.dart';
import '../../function/utils/my_common.dart';

/// 设置页面组件
/// 使用非常规命名避免标准模式
class SettingsPage extends GetView<SettingsPageController> {
  const SettingsPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/images/communal_bg.png'),
            fit: BoxFit.fill,
          ),
        ),
        child: SafeArea(
          child: Column(
            children: [
              // 顶部导航栏
              _buildTopBar(),

              // 可滚动的内容区域
              Expanded(
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      // 用户头像区域
                      _buildAvatarSection(),

                      // 用户信息编辑区域
                      _buildUserInfoSection(),

                      // 设置选项区域
                      _buildSettingsSection(),

                      // 登出按钮
                      _buildLogoutButton(),

                      SizedBox(height: 40.h),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// 构建顶部导航栏
  Widget _buildTopBar() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 16.h),
      child: Row(
        children: [
          // 返回按钮
          Icon(
            Icons.arrow_back_ios,
            color: MyCommonUtils.white,
            size: 24.sp,
          ).inkWell(onTap: controller.goBack),

          SizedBox(width: 10.w),

          // 页面标题
          Expanded(
            child: Text(
              'setting',
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 24.sp,
                fontWeight: FontWeight.bold,
                color: MyCommonUtils.white,
                letterSpacing: 1.2,
                fontFamily:
                    GoogleFonts.berkshireSwash(
                      fontWeight: FontWeight.w400,
                    ).fontFamily,
              ),
            ),
          ),

          // 保存按钮
          Obx(() {
            return controller.isSaving
                ? CircularProgressIndicator(
                  strokeWidth: 2,
                  valueColor: AlwaysStoppedAnimation<Color>(
                    MyCommonUtils.white,
                  ),
                ).center()
                : Image.asset(
                  'assets/images/save.png',
                  width: 24.w,
                  height: 24.h,
                ).inkWell(onTap: controller.saveUserInfo);
          }),
        ],
      ),
    );
  }

  /// 构建头像区域
  Widget _buildAvatarSection() {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 30.h),
      child: Column(
        children: [
          // 头像
          Obx(() {
            return GestureDetector(
              onTap: controller.selectAvatar,
              child: Container(
                width: 80.w,
                height: 80.w,
                decoration: BoxDecoration(shape: BoxShape.circle),
                child: ClipOval(child: _buildAvatarImage()),
              ),
            );
          }),
        ],
      ),
    );
  }

  /// 构建头像图片
  Widget _buildAvatarImage() {
    final String avatarPath = controller.editedAvatar;

    if (avatarPath.startsWith('assets/')) {
      // 资源图片
      return Image.asset(
        avatarPath,
        width: 120.w,
        height: 120.w,
        fit: BoxFit.cover,
      );
    } else if (File(avatarPath).existsSync()) {
      // 本地文件
      return Image.file(
        File(avatarPath),
        width: 120.w,
        height: 120.w,
        fit: BoxFit.cover,
      );
    } else {
      // 默认头像
      return Image.asset(
        'assets/images/avatar1.png',
        width: 120.w,
        height: 120.w,
        fit: BoxFit.cover,
      );
    }
  }

  /// 构建用户信息编辑区域
  Widget _buildUserInfoSection() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 20.w),
      child: Column(
        children: [
          // 姓名编辑
          _buildNameField(),

          SizedBox(height: 24.h),

          // 签名编辑
          _buildSignatureField(),
        ],
      ),
    );
  }

  /// 构建姓名编辑字段
  Widget _buildNameField() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          'Name',
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w600,
            color: MyCommonUtils.white,
          ),
        ),
        SizedBox(height: 8.h),
        Obx(() {
          return Container(
            decoration: BoxDecoration(
              color: MyCommonUtils.white,
              borderRadius: BorderRadius.circular(12.r),
            ),
            child: TextField(
              controller: TextEditingController(text: controller.editedNickname)
                ..selection = TextSelection.collapsed(
                  offset: controller.editedNickname.length,
                ),
              onChanged: controller.updateNickname,
              style: TextStyle(fontSize: 16.sp, color: MyCommonUtils.black),
              decoration: InputDecoration(
                hintText: 'Ruben',
                hintStyle: TextStyle(
                  fontSize: 16.sp,
                  color: MyCommonUtils.grey,
                ),
                border: InputBorder.none,
                contentPadding: EdgeInsets.symmetric(
                  horizontal: 16.w,
                  vertical: 12.h,
                ),
                suffixIcon: Icon(
                  Icons.edit,
                  color: MyCommonUtils.grey,
                  size: 18.sp,
                ),
              ),
            ),
          );
        }),
      ],
    );
  }

  /// 构建签名编辑字段
  Widget _buildSignatureField() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          'Tags',
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w600,
            color: MyCommonUtils.white,
          ),
        ),
        SizedBox(height: 8.h),
        Obx(() {
          return Container(
            decoration: BoxDecoration(
              color: MyCommonUtils.white,
              borderRadius: BorderRadius.circular(12.r),
            ),
            child: TextField(
              controller: TextEditingController(
                  text: controller.editedSignature,
                )
                ..selection = TextSelection.collapsed(
                  offset: controller.editedSignature.length,
                ),
              onChanged: controller.updateSignature,
              maxLines: 3,
              style: TextStyle(fontSize: 16.sp, color: MyCommonUtils.black),
              decoration: InputDecoration(
                hintText: 'please enter your personalized signature !',
                hintStyle: TextStyle(
                  fontSize: 16.sp,
                  color: MyCommonUtils.grey,
                ),
                border: InputBorder.none,
                contentPadding: EdgeInsets.symmetric(
                  horizontal: 16.w,
                  vertical: 12.h,
                ),
                suffixIcon: Icon(
                  Icons.edit,
                  color: MyCommonUtils.grey,
                  size: 18.sp,
                ),
              ),
            ),
          );
        }),
      ],
    );
  }

  /// 构建设置选项区域
  Widget _buildSettingsSection() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 20.w, vertical: 20.h),
      child: Column(
        children: [
          _buildSettingItem(
            'Terms Of Service',
            Icons.description_outlined,
            controller.goToTermsOfService,
          ),
          _buildSettingItem(
            'Eula',
            Icons.article_outlined,
            controller.goToEula,
          ),
          _buildSettingItem(
            'Delete account',
            Icons.delete_outline,
            controller.deleteAccount,
            isDestructive: true,
          ),
        ],
      ),
    );
  }

  /// 构建设置项
  Widget _buildSettingItem(
    String title,
    IconData icon,
    VoidCallback onTap, {
    bool isDestructive = false,
  }) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 0.w, vertical: 16.h),
      child: Row(
        children: [
          Expanded(
            child: Text(
              title,
              style: TextStyle(
                fontSize: 17.sp,
                fontWeight: FontWeight.w500,
                color: MyCommonUtils.white,
              ),
            ),
          ),
          Icon(
            Icons.arrow_forward_ios,
            color: MyCommonUtils.white,
            size: 16.sp,
          ),
        ],
      ).inkWell(onTap: onTap),
    );
  }

  /// 构建登出按钮
  Widget _buildLogoutButton() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 63.w, vertical: 8.h),
      margin: EdgeInsets.symmetric(horizontal: 80.w),
      decoration: ShapeDecoration(
        gradient: LinearGradient(
          begin: Alignment(0.00, 1.00),
          end: Alignment(1.01, -0.00),
          colors: [const Color(0xFFFF8D00), const Color(0xFF00FFB2)],
        ),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(100.r),
        ),
      ),
      child: Text(
        'Log Out',
        textAlign: TextAlign.center,
        style: TextStyle(
          fontSize: 18.sp,
          fontWeight: FontWeight.bold,
          color: MyCommonUtils.white,
        ),
      ).center().inkWell(onTap: controller.logout),
    );
  }
}
